package com.igame.maingame;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsSpace;

public class SoldierBallManager {
	private Map<String, DynamicPhysicsNode> balllist = new TreeMap<String, DynamicPhysicsNode>();
	int ballid = 0;
	private PhysicsSpace physpace;
	private Node scene;
	private Node allparent;
	private PhysicsSpace getPhysicsSpace(){
		return physpace;
	}
	
	public void clear(){
		for(DynamicPhysicsNode node:balllist.values()){
			node.delete();
		}
		updateScene();
		balllist.clear();
	}
	
	public void destroyBall(String name){
		DynamicPhysicsNode node = balllist.get(name);
		node.delete();
		updateScene();
		balllist.remove(name);
	}
	
	public SoldierBallManager( Node sceneroot, PhysicsSpace physicsSpace ){
		scene = sceneroot;
		physpace = physicsSpace;
		
		allparent = new Node("itemballroot");
		scene.attachChild(allparent);
		updateScene();
	}
	
	private float def_ballsize = 0.7f;
	private float mass = 1.0f;
	public void addBall(Vector3f pos,ControlBall owner){
		SoldierBall ball = new SoldierBall(String.format("ball_%d", ballid++ ),10,10,def_ballsize);
		DynamicPhysicsNode node = getPhysicsSpace().createDynamicNode();
		node.attachChild(ball);
		node.setName(ball.getName());
		node.generatePhysicsGeometry();
		node.setLocalTranslation(pos);
		node.setUserData(OBJECT_TYPE.class.getName(), OBJECT_TYPE.SOLDIER_BALL);
		ball.setOwner(owner);
		mass = node.getMass();
		allparent.attachChild(node);
		updateScene();
		
		balllist.put(node.getName(),node);
	}
	
	public static SoldierBall getSoldierBallFromNode( Node node ){
		Object obj = node.getChild(0);
		if(obj!=null && obj instanceof SoldierBall)
			return (SoldierBall)obj;
		else return null;
	}
	
	public SoldierBall getOneBall(){
		if(balllist.size() > 0)
			return (SoldierBall)balllist.values().iterator().next().getChild(0);
		else return null;
	}
	
	public void updateForce( Vector3f source ){
		for(DynamicPhysicsNode node:balllist.values()){
			Vector3f pos = node.getLocalTranslation().clone();
			Vector3f force = calcForce(pos,source);
			node.addForce(force);
		}
	}
	
	float staticForceScale = -20;
	private Vector3f calcForce(Vector3f pos, Vector3f source){
		float dist2 = pos.distance(source);
		if(dist2 < 4)
			return Vector3f.ZERO;
		float forcescale = (float)Math.exp(-dist2/30);
		return pos.add(source.negate()).normalize().mult(forcescale * staticForceScale * mass/1.2f);
	}
	
	private void updateScene(){
		scene.updateGeometricState(0, true);
		scene.updateRenderState();
	}
	
	public class SoldierBall extends Sphere{
		private boolean is_picked = false;
		private ControlBall owner;
		public void setOwner(ControlBall cb){
			owner = cb;
		}
		public ControlBall getOwner(){
			return owner;
		}
	    public SoldierBall(String name, Vector3f center, int zSamples,
	            int radialSamples, float radius, boolean useEvenSlices){
	    	super(name,center,zSamples,radialSamples,radius,useEvenSlices);
	    }
	    public SoldierBall(String name, int zSamples, int radialSamples, float radius) {
	        this(name, new Vector3f(0, 0, 0), zSamples, radialSamples, radius);
	    }
	    public SoldierBall(String name, Vector3f center, int zSamples,
	            int radialSamples, float radius) {
	        this(name, center, zSamples, radialSamples, radius, false);
	    }
	}
}
